Skip to content

chore: Plumb query results to graph server/colab callback via python globals, rather than html/javascript#205

Closed
ericfe-google wants to merge 5 commits intomainfrom
query
Closed

chore: Plumb query results to graph server/colab callback via python globals, rather than html/javascript#205
ericfe-google wants to merge 5 commits intomainfrom
query

Conversation

@ericfe-google
Copy link
Contributor

chore: Plumb query results to graph server/colab callback via python globals, rather than html/javascript

This improves allows the graph visualizer to handle larger query results without running out of memory, due to the generated html being too large.

@ericfe-google ericfe-google requested review from a team as code owners January 28, 2026 22:35
@ericfe-google ericfe-google requested a review from jialuoo January 28, 2026 22:35
@product-auto-label product-auto-label bot added size: s Pull request size is small. api: bigquery Issues related to the googleapis/python-bigquery-magics API. labels Jan 28, 2026
@gemini-code-assist
Copy link

Summary of Changes

Hello @ericfe-google, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant architectural change in how query results are handled within the BigQuery magics and graph visualization components. By transitioning from passing query results through HTML/JavaScript parameters to utilizing Python global variables, the system can now efficiently manage and visualize much larger datasets. This refactoring directly addresses and mitigates potential out-of-memory errors that previously occurred with extensive query outputs, thereby enhancing the stability and scalability of the graph visualizer.

Highlights

  • Data Transfer Mechanism Change: Query results are now passed via Python global variables instead of being embedded in HTML/JavaScript parameters, which previously led to memory issues with large datasets.
  • Memory Optimization: This refactoring prevents out-of-memory errors when handling large query results, significantly improving the robustness and scalability of the graph visualizer.
  • Graph Server Integration: The GraphServer class now directly stores the query_result, and both the Colab callback and the graph server's query handler retrieve data from this new attribute.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the graph visualization feature to pass query results via a global Python variable instead of embedding them in HTML. This is a good change to support larger query results and avoid browser memory issues.

However, the current implementation introduces a race condition by using a single global variable to store the result. If multiple cells generating graphs are run concurrently, this could lead to incorrect data being displayed. I've suggested a more robust approach using unique IDs for each query to make the process stateless.

I've also pointed out a couple of minor issues with unused variables and dead code that could be cleaned up.

singleton_server_thread = graph_server.graph_server.init()
port = graph_server.graph_server.port

graph_server.graph_server.query_result = query_result

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using a single global variable graph_server.graph_server.query_result to pass data to the graph visualizer introduces a potential race condition. If two notebook cells that generate graphs are run in quick succession, the second cell could overwrite query_result before the first cell's visualization has had a chance to fetch its data, leading to the first visualization displaying the second one's data.

A more robust solution would be to avoid this shared state. For example, you could store query results in a dictionary keyed by a unique ID:

  1. Generate a unique ID for each query.
  2. Store the query result in a dictionary on the graph_server object, e.g., graph_server.graph_server.results[query_id] = query_result.
  3. Pass this query_id to the frontend via the params argument of generate_visualization_html.
  4. The frontend callback would then include this query_id when calling back to Python, allowing _colab_query_callback and handle_post_query to retrieve the correct result.

This would make the visualization mechanism stateless and safe for concurrent executions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a bad suggestion, but I have not encountered a notebook environment that allows more than one cell to run at a time, so it's not a huge concern for me.

@@ -633,8 +633,9 @@ def _handle_result(result, args):


def _colab_query_callback(query: str, params: str):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The query and params arguments are no longer used in this function. It's a good practice to prefix unused variables with an underscore to make the code clearer.

Suggested change
def _colab_query_callback(query: str, params: str):
def _colab_query_callback(_query: str, _params: str):

@@ -251,7 +252,9 @@ def handle_post_ping(self):

def handle_post_query(self):
data = self.parse_post_data()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The data variable is assigned but its value is never used. This line can be removed.

@product-auto-label product-auto-label bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Jan 29, 2026
@ericfe-google ericfe-google assigned tswast and unassigned ohmayr Jan 29, 2026
@ericfe-google ericfe-google deleted the query branch February 2, 2026 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: bigquery Issues related to the googleapis/python-bigquery-magics API. size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants